package com.simplemall.micro.serv.tel.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.simplemall.micro.serv.common.bean.telbook.TelBookInfo;
import com.simplemall.micro.serv.common.util.ChineseCharToEnUtil;
import com.simplemall.micro.serv.tel.mapper.TelBookInfoMapper;
import com.simplemall.micro.serv.tel.service.TelBookInfoService;

@Service
public class TelBookInfoSerivceImpl implements TelBookInfoService{

	@Autowired
	TelBookInfoMapper telBookInfoMapper;
	
	private Logger logger = LoggerFactory.getLogger(TelBookInfoSerivceImpl.class);

	@Override
	public boolean insertTelBookInfo(String telBookInfoStr) {
		JSONObject jsonObject = null;
		try {
			if (StringUtils.isEmpty(telBookInfoStr)) {
				//mock data to generate order data
				return false;
			}
			jsonObject = JSONObject.parseObject(telBookInfoStr);
		} catch (Exception exception) {
			logger.error("用户数据解析异常{}", exception);
			return false;
		}
		TelBookInfo telBookInfo = JSONObject.toJavaObject(jsonObject, TelBookInfo.class);
		telBookInfo.setFpinyin(ChineseCharToEnUtil.getAllFirstLetter(telBookInfo.getFname()));
		int result = telBookInfoMapper.insert(telBookInfo);
		return result > 1? true : false ;
	}

	@Override
	public boolean deleteTelBookInfo(String id) {
		int result = telBookInfoMapper.deleteByPrimaryKey(id);
		return result > 1? true : false ;
	}

	@Override
	public boolean updateTelBookInfo(String telBookInfoStr) {
		JSONObject jsonObject = null;
		try {
			if (StringUtils.isEmpty(telBookInfoStr)) {
				//mock data to generate order data
				return false;
			}
			jsonObject = JSONObject.parseObject(telBookInfoStr);
		} catch (Exception exception) {
			logger.error("用户数据解析异常{}", exception);
			return false;
		}
		TelBookInfo telBookInfo = JSONObject.toJavaObject(jsonObject, TelBookInfo.class);
		return telBookInfoMapper.updateByPrimaryKey(telBookInfo) > 1 ? true : false;
	}

	@Override
	public TelBookInfo selectTelBookInfoByExample(TelBookInfo telBookInfo) {
		return telBookInfoMapper.selectByUserid(telBookInfo.getfUserid(), telBookInfo.getfUserid());
	}

	@Override
	public List<TelBookInfo> selectTelBookInfoByTelBookId(String telBookId) {
		// TODO Auto-generated method stub
		return telBookInfoMapper.selectByTelBookId(telBookId);
	}

	@Override
	public TelBookInfo getTelBookInfoByFuserid(String telBookId, String fuserid) {
		// TODO Auto-generated method stub
		return telBookInfoMapper.getTelBookInfoByFuserid(telBookId, fuserid);
	}

}
